package com.ruoyi.mes.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.mes.domain.HouInfo;
import com.ruoyi.mes.mapper.HouInfoMapper;
import com.ruoyi.mes.utils.RepeatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.mapper.LocatorInfoMapper;
import com.ruoyi.mes.domain.LocatorInfo;
import com.ruoyi.mes.service.ILocatorInfoService;

/**
 * 库位信息Service业务层处理
 *
 * @author LongAndHao
 * @date 2023-09-27
 */
@Service
public class LocatorInfoServiceImpl implements ILocatorInfoService
{
    @Autowired
    private LocatorInfoMapper locatorInfoMapper;

    @Autowired
    private HouInfoMapper houInfoMapper;

    /**
     * 查询库位信息
     *
     * @param id 库位信息主键
     * @return 库位信息
     */
    @Override
    public LocatorInfo selectLocatorInfoById(Long id)
    {
        return locatorInfoMapper.selectLocatorInfoById(id);
    }

    /**
     * 查询库位信息列表
     *
     * @param locatorInfo 库位信息
     * @return 库位信息
     */
    @Override
    public List<LocatorInfo> selectLocatorInfoList(LocatorInfo locatorInfo)
    {
        return locatorInfoMapper.selectLocatorInfoList(locatorInfo);
    }

    /**
     * 新增库位信息
     *
     * @param locatorInfo 库位信息
     * @return 结果
     */
    @Override
    public AjaxResult insertLocatorInfo(LocatorInfo locatorInfo)
    {

        if (RepeatUtils.isLocatorCoderRepeat(locatorInfoMapper,locatorInfo)){
            return AjaxResult.error("库位编码重复");
        }

        HouInfo houInfo = houInfoMapper.selectHouInfoByHouId(locatorInfo.getHouId());
        if (locatorInfo.getMaxiCapacity() > houInfo.getHouCapacity()){
            return AjaxResult.error("容量超限！");
        }

        locatorInfo.setCreateTime(DateUtils.getNowDate());
        locatorInfoMapper.insertLocatorInfo(locatorInfo);
        return AjaxResult.success("新增成功!");
    }

    /**
     * 修改库位信息
     *
     * @param locatorInfo 库位信息
     * @return 结果
     */
    @Override
    public int updateLocatorInfo(LocatorInfo locatorInfo)
    {
        locatorInfo.setUpdateTime(DateUtils.getNowDate());
        return locatorInfoMapper.updateLocatorInfo(locatorInfo);
    }

    /**
     * 批量删除库位信息
     *
     * @param ids 需要删除的库位信息主键
     * @return 结果
     */
    @Override
    public AjaxResult deleteLocatorInfoByIds(Long[] ids)
    {

        for (Long id : ids) {
            LocatorInfo locatorInfo = locatorInfoMapper.selectLocatorInfoById(id);
            if (locatorInfo.getRemainCapacity() < locatorInfo.getMaxiCapacity()){
                return AjaxResult.error("库位中还有物品!");
            }
        }
        locatorInfoMapper.deleteLocatorInfoByIds(ids);
        return AjaxResult.success("删除成功!");
    }

    /**
     * 删除库位信息信息
     *
     * @param id 库位信息主键
     * @return 结果
     */
    @Override
    public int deleteLocatorInfoById(Long id)
    {
        return locatorInfoMapper.deleteLocatorInfoById(id);
    }

    /**
     * 查 - 库位
     * @param houId 库房id
     * @return
     */
    @Override
    public AjaxResult getLocatorByHouId(Long houId) {
        List<LocatorInfo> list = locatorInfoMapper.getLocatorByHouId(houId);
        return AjaxResult.success(list);
    }
}
